package com.carleasoft.mps.qa.business.anwser.controller;

import cn.hutool.http.HttpRequest;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carleasoft.mps.core.result.R;
import com.carleasoft.mps.core.utils.IBeanUtil;
import com.carleasoft.mps.qa.business.anwser.bean.entity.NaireEntity;
import com.carleasoft.mps.qa.business.anwser.bean.entity.NaireRecordEntity;
import com.carleasoft.mps.qa.business.anwser.bean.po.NairePagePo;
import com.carleasoft.mps.qa.business.anwser.bean.po.PageAnswerPo;
import com.carleasoft.mps.qa.business.anwser.bean.vo.AnswerPageQueryVo;
import com.carleasoft.mps.qa.business.anwser.bean.vo.PageAnswerVo;
import com.carleasoft.mps.qa.business.anwser.bean.vo.ScoreCountVo;
import com.carleasoft.mps.qa.business.anwser.service.NaireService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: yangHao
 * @createDate:2023/5/24
 * @description:
 */
@RestController
@RequestMapping("/naire")
public class NaireController {
    @Autowired
    private NaireService naireService;


    /**
     * 添加/修改问卷
     *
     * @param naireEntity
     * @return
     */
    @PostMapping("/saveAndUpdate")
    public R save(@RequestBody NaireEntity naireEntity) {
        boolean isOk = naireService.saveNaire(naireEntity);
        return R.ok(isOk);
    }

    /**
     * 复制
     *
     * @return
     */
    @PostMapping("/copy")
    public R copy(@RequestBody NaireEntity naireEntity) {
        IBeanUtil.paramVerify(naireEntity, "naireCode");
        boolean isOk = naireService.copy(naireEntity);
        return R.ok(isOk);
    }

    /**
     * 修改状态
     *
     * @param naireEntity
     * @return
     */
    @PostMapping("/updateStatus")
    public R updateStatus(@RequestBody NaireEntity naireEntity) {
        IBeanUtil.paramVerify(naireEntity, "naireCode", "status");
        boolean update = naireService.update(Wrappers.<NaireEntity>lambdaUpdate()
                .eq(NaireEntity::getNaireCode, naireEntity.getNaireCode())
                .set(NaireEntity::getStatus, naireEntity.getStatus()));
        return R.ok(update);
    }

    /**
     * 分页
     *
     * @param nairePagePo
     * @return
     */
    @PostMapping("/pageQuery")
    public R pageQuery(@RequestBody NairePagePo nairePagePo) {
        Page<NaireEntity> naireEntityPage = naireService.pageQuery(new Page(nairePagePo.getCurrent(), nairePagePo.getSize()), nairePagePo);
        return R.ok(naireEntityPage);
    }

    /**
     * 删除
     *
     * @param naireEntity
     * @return
     */
    @PostMapping("/delete")
    public R delete(@RequestBody NaireEntity naireEntity) {
        boolean isOk = naireService.delete(naireEntity);
        return R.ok(isOk);
    }

    /**
     * 详情
     *
     * @param naireEntity
     * @return
     */
    @PostMapping("/detail")
    public R detail(@RequestBody NaireEntity naireEntity) {
        IBeanUtil.paramVerify(naireEntity, "naireCode");
        NaireEntity entity = naireService.detail(naireEntity);
        return R.ok(entity);
    }

    /**
     * 问卷提交
     *
     * @param naireRecordEntity
     * @return
     */
    @PostMapping("/submit")
    public R submit(@RequestBody NaireRecordEntity naireRecordEntity, HttpServletRequest request) {
        boolean isOk = naireService.submit(naireRecordEntity, request);
        return R.ok(isOk);
    }

    /**
     * 答题情况-分页
     *
     * @return
     */
    @PostMapping("/answerPageQuery")
    public R answerPageQuery(@RequestBody NairePagePo nairePagePo) {
        Page<AnswerPageQueryVo> answerPageQueryVoPage = naireService.answerPageQuery(new Page(nairePagePo.getCurrent(), nairePagePo.getSize()), nairePagePo);
        return R.ok(answerPageQueryVoPage);
    }

    /**
     * 根据问卷编码查询用户成绩-分页
     *
     * @param pageAnswerPo
     * @return
     */
    @PostMapping("/pageAnswer")
    public R pageAnswer(@RequestBody PageAnswerPo pageAnswerPo) {
        IBeanUtil.paramVerify(pageAnswerPo, "naireCode", "current", "size");
        Page<PageAnswerVo> page = naireService.pageAnswer(new Page(pageAnswerPo.getCurrent(), pageAnswerPo.getSize()), pageAnswerPo);
        return R.ok(page);
    }

    /**
     * 根据recordCode 删除用户成绩
     *
     * @param pageAnswerPo
     * @return
     */
    @PostMapping("/deleteUserAnswer")
    public R deleteUserAnswer(@RequestBody PageAnswerPo pageAnswerPo) {
        IBeanUtil.paramVerify(pageAnswerPo, "recordCode");
        boolean isOk = naireService.deleteUserAnswer(pageAnswerPo);
        return R.ok(isOk);
    }

    /**
     * 查询问卷集合
     *
     * @return
     */
    @PostMapping("/list")
    public R list() {
        List<NaireEntity> naireEntities = naireService.list(Wrappers.<NaireEntity>lambdaQuery()
                .in(NaireEntity::getStatus, 1, 3)
                .orderByDesc(NaireEntity::getCreateTime));
        return R.ok(naireEntities);
    }

    /**
     * 根据问卷编码统计成绩层级
     *
     * @return
     */
    @PostMapping("/scoreCount")
    public R scoreCount(@RequestBody NaireEntity naireEntity) {
        IBeanUtil.paramVerify(naireEntity, "naireCode");
        ScoreCountVo scoreCountVo = naireService.scoreCount(naireEntity);
        return R.ok(scoreCountVo);
    }


}
